package CouponController

import (
	"QingDai-Gin/api/entities"
	"QingDai-Gin/api/models"
	"QingDai-Gin/service/CouponService"
	"QingDai-Gin/service/ShopService"
	"QingDai-Gin/utils/JwtUtil"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

// GetCouponByJwt
// @Tags 优惠卷
// @Summary 根据jwt用户获取自己的优惠卷
// @Router /coupon/jwt [get]
func GetCouponByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	coupons, _ := CouponService.GetCouponsByL(map[string]any{"user_list": UserId})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据jwt用户获取自己的优惠卷成功", Data: coupons})
}

// GetCouponByShopId
// @Tags 优惠卷
// @Summary 根据shopId获取优惠卷
// @Router /coupon/shopId [get]
func GetCouponByShopId(context *gin.Context) {
	shopId, _ := context.Get("shopId")
	//UserId, err := JwtUtil.GetUserIdByContext(context)
	//if err != nil {
	//	context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	//}
	coupons, _ := CouponService.GetCouponsByP(map[string]any{"shop_id": shopId})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据shopId获取优惠卷成功", Data: coupons})
}

// CreateCouponByShopIdAndJwt
// @Tags 优惠卷
// @Summary 根据shopId及jwt给指定商店创建优惠券
// @Router /coupon/jwt/shopId [post]
func CreateCouponByShopIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	shopId, _ := context.Get("shopId")
	rule, _ := context.Get("rule")
	ruleDetail, _ := context.Get("ruleDetail")
	time, _ := context.Get("time")
	userList, _ := context.Get("userList")
	shop, _ := ShopService.GetShopById(shopId.(int))
	if shop.SellerId != UserId {
		context.JSON(200, entities.CommonResult{Code: 400, Message: "不具备权限给该商店创建优惠券权限", Data: nil})
	}
	coupon := models.Coupon{
		ShopId:     shopId.(int),
		Rule:       rule.(string),
		RuleDetail: ruleDetail.(string),
		Time:       time.(string),
		UserList:   userList.(string),
		Enable:     1,
	}
	CouponService.CreateCoupon(coupon)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据shopId及jwt给指定商店创建优惠券成功", Data: coupon})
}

// UpdateCouponByIdAndJwt
// @Tags 优惠卷
// @Summary 根据id及jwt更新优惠卷
// @Router /coupon/jwt/id [put]
func UpdateCouponByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}

	id, _ := context.Get("Id")
	shopId, _ := context.Get("shopId")
	rule, _ := context.Get("rule")
	ruleDetail, _ := context.Get("ruleDetail")
	time, _ := context.Get("time")
	userList, _ := context.Get("userList")

	couponGet, _ := CouponService.GetCouponById(id.(int))
	if couponGet.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "优惠卷id非法", Data: nil})
	}
	shop, _ := ShopService.GetShopById(couponGet.ShopId)
	if shop.SellerId != UserId {
		context.JSON(200, entities.CommonResult{Code: 400, Message: "不具备权限给该商店创建优惠券权限", Data: nil})
	}
	coupon := models.Coupon{
		ID:         id.(int),
		ShopId:     shopId.(int),
		Rule:       rule.(string),
		RuleDetail: ruleDetail.(string),
		Time:       time.(string),
		UserList:   userList.(string),
	}
	CouponService.UpdateCoupon(coupon)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id及jwt更新优惠卷成功", Data: nil})
}

// GetCouponByIdAndJwt
// @Tags 优惠卷
// @Summary 根据id及jwt获取优惠卷
// @Router /coupon/jwt/id [get]
func GetCouponByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("Id")
	coupon, _ := CouponService.GetCouponById(id.(int))
	if coupon.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "优惠卷id非法", Data: nil})
	}
	shop, _ := ShopService.GetShopById(coupon.ShopId)
	if shop.SellerId != UserId || strings.Contains(coupon.UserList, strconv.Itoa(UserId)) {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备查询该id优惠卷权限", Data: nil})
	}
	context.JSON(200, entities.CommonResult{Code: 200, Message: "获取优惠卷成功", Data: coupon})
}

// DeleteCouponByIdAndJwt
// @Tags 优惠卷
// @Summary 根据id及jwt删除优惠卷
// @Router /coupon/jwt/id [delete]
func DeleteCouponByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("Id")
	coupon, _ := CouponService.GetCouponById(id.(int))
	if coupon.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "优惠卷id非法", Data: nil})
	}
	shop, _ := ShopService.GetShopById(coupon.ShopId)
	if shop.SellerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备删除该id优惠卷权限", Data: nil})
	}
	CouponService.DeleteCoupon(coupon)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "获取优惠卷成功", Data: nil})
}
